<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');




class Fb extends CI_Controller {

	/*
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function __construct()
    {

		parent::__construct();

		(!session_id()) ? session_start(): '';

		$this->load->helper("myuser_helper");



    }

	public function index()
	{

		echo "THIS APPLICATION IS CREATED BY ADVANZWEB.COM ";

	}

	public function support(){

		echo "supports";
	}

	public function policy(){

		echo "policy";
	}

	public function canvas(){

		echo "sample app page";

	}

	public function signup(){

		$facebook = new FacebookAccessLib();
		$user = $facebook->getUser();

		$fql = 'SELECT first_name, last_name, email,middle_name, username, birthday_date, about_me from user where uid = me() ';
		$ret_obj = $facebook->api(array(
						  'method' => 'fql.query',
						  'query' => $fql,
						));
		$fb_datas = $ret_obj[0];
		$fb_email  = $fb_datas['email'];


	}

	public function login(){

	$facebook = new FacebookAccessLib();
	$user = $facebook->getUser();

	if($user){
				 $fql = 'SELECT first_name, last_name, email,middle_name, username, birthday_date, about_me from user where uid = me() ';
				 $ret_obj = $facebook->api(array(
						  'method' => 'fql.query',
						  'query' => $fql,
						));
				 $fb_datas = $ret_obj[0];
				 $fb_email  = $fb_datas['email'];


				 //sytem user id
				 $qry_check_user = $this->db->query("select email from mlme_users where email='$fb_email' limit 1");
				 $found_user = $qry_check_user->num_rows();



					 //Check if the email is existing on the website database or not.
					 if($found_user){


							 $found_userinfo = $qry_check_user->row();

							 $_SESSION['email'] = $found_userinfo->email;
							 $_SESSION['is_logged_in'] = 1;

							redirect("profile","refresh");




					 }
					 else{ // user_id = 0

						 $first_name  = $fb_datas['first_name'];
						 $last_name   = $fb_datas['last_name'];
						 $middle_name = $fb_datas['middle_name'];
						 $type       = 'free';
						 $online      = 1;
						 $status      = 1;
						 $about       = $fb_datas['about_me'];
						 $password    = Myuser_helper::randLetterNumber();
						 $username    = $fb_datas['username'];

						 $b_arr       = explode("/",$fb_datas['birthday_date']);
						 $birth_day   = date("Y-m-d",mktime(0,0,0,$b_arr[0],$b_arr[1],$b_arr[2]));

						 $picture     = "";


						 //user model loading
				         $this->load->model('users_model');


						 $save = $this->db->simple_query("insert into mlme_users value('','$first_name','$last_name','$middle_name','$username',
		                                                   '$fb_email',sha1(md5('$password')),'$birth_day',
														   '$picture',now(),'0000-00-00 00:00:00',
														   '$about','$type','$online','$status')");

						 if($save){

						    $_SESSION['email'] = $fb_email;
							$_SESSION['is_logged_in'] = 1;

							redirect("profile","refresh");
						 }



					 }



	}
	else redirect("home","refresh");



	}


	public function terms(){

	echo "terms";
	}

	public function logout(){

		session_destroy();

		redirect("fb/login","refresh");

	}




}
